<?php
session_start();
require_once("../config/include_tokyo.inc.php");

//访问权限验证
$userId = $_SESSION['user_id'];
$userRole = $_SESSION['user_role'];
if($userRole == 1 ){
	//管理员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}


//客户详情 
$id = $_GET['id'];
if($id == ""){
	$id = $_POST['id'];
}
if($id == ""){
	$id = $_POST['editId'];
}
if($id == ""){
	//新增客户
}else{
	//编辑客户
	$sql = "SELECT * from customers where id=".$id;
	$result = $db->Execute($sql);
}



$doFlag = $_POST["doFlag"];
if($doFlag == "insertCustomer"){
	//保存
	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$email_address = $_POST['email_address'];
	$street_address = $_POST['street_address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$country = $_POST['country'];
	$postcode = $_POST['postcode'];
	$tel_number = $_POST['tel_number'];
	$vip_level = $_POST['vip_level'];
	$site_name = $_POST['site_name'];
	$product_name = $_POST['product_name'];
	$note = $_POST['note'];
	$sql_insert_customer = "INSERT INTO customers
							 (firstname,lastname,email_address,street_address,city,state,country,postcode,tel_number,vip_level,site_name,product_name,note,create_time) 
							 values
							 ('$firstname','$lastname','$email_address','$street_address','$city','$state','$country','$postcode','$tel_number',$vip_level,'$site_name','$product_name','$note',Now())";
	$db->Execute($sql_insert_customer);	
}else if($doFlag == "updateCustomer"){
	//更新客户信息 
	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$email_address = $_POST['email_address'];
	$street_address = $_POST['street_address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$country = $_POST['country'];
	$postcode = $_POST['postcode'];
	$tel_number = $_POST['tel_number'];
	$vip_level = $_POST['vip_level'];
	$site_name = $_POST['site_name'];
	$product_name = $_POST['product_name'];
	$note = $_POST['note'];

	$sql = "update customers set firstname = '".$firstname."'";
	$sql = $sql.", lastname='".$lastname."'";
	$sql = $sql.", email_address='".$email_address."'";
	$sql = $sql.", street_address='".$street_address."'";
	$sql = $sql.", city='".$city."'";
	$sql = $sql.", state='".$state."'";
	$sql = $sql.", country='".$country."'";
	$sql = $sql.", postcode='".$postcode."'";
	$sql = $sql.", tel_number='".$tel_number."'";
	$sql = $sql.", vip_level=".$vip_level;
	$sql = $sql.", site_name='".$site_name."'";
	$sql = $sql.", product_name='".$product_name."'";
	$sql = $sql.", note='".$note."'";
	$sql = $sql."  where id=".$id;	
	$db->Execute($sql);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "客户详情"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<form name="inputForm" action="../customer/customerInput.php" method="post">
<div id="container" >	
<div class="grayTable">	
<h4>
<?php
if($doFlag == "updateCustomer"){
	echo "客户资料更新成功!";
}else if($id == ""){
	echo "新增客户资料";
}else{
	echo "编辑客户资料";
}
?>
</h4>
<table >
<thead>
	<tr>
		<th scope="col" width=90 >字段名称</th>
	    <th scope="col" >字段值</th>		
	</tr>
</thead>

<?php
if($id == ""){
	$strTR = "";
	$strTR = $strTR."<tr ><td>First Name</td>";
	$strTR = $strTR."<td><input type='text' name='firstname' id='firstname' value='' size=64></td></tr>";
	$strTR = $strTR."<tr ><td>Last Name</td>";
	$strTR = $strTR."<td><input type='text' name='lastname' id='lastname' value='' size=64></td></tr>";
	$strTR = $strTR."<tr ><td>Email</td>";
	$strTR = $strTR."<td><input type='text' name='email_address' id='email_address' value='' size=64></td></tr>";
	$strTR = $strTR."<tr ><td>Street Address</td>";
	$strTR = $strTR."<td><input type='text' name='street_address' id='street_address' value='' size=64></td></tr>";
	$strTR = $strTR."<tr ><td>City</td>";
	$strTR = $strTR."<td><input type='text' name='city' id='city' value='' size=64></td></tr>";
	$strTR = $strTR."<tr ><td>State</td>";
	$strTR = $strTR."<td><input type='text' name='state' id='state' value='' size=64></td></tr>";
	$strTR = $strTR."<tr ><td>Country</td>";
	$strTR = $strTR."<td><input type='text' name='country' id='country' value='' size=64></td></tr>";
	$strTR = $strTR."<tr ><td>Post code</td>";
	$strTR = $strTR."<td><input type='text' name='postcode' id='postcode' value='' size=64></td></tr>";
	$strTR = $strTR."<tr ><td>Telephone Number</td>";
	$strTR = $strTR."<td><input type='text' name='tel_number' id='tel_number' value='' size=64></td></tr>";
	$strTR = $strTR."<tr ><td>VIP Level</td>";
	$strTR = $strTR."<td><select name='vip_level' id='vip_level'>
	                     <option value=1 >★ 下单>2次or>$500</option>
						 <option value=2 >★★ 下单>5次</option>
						 <option value=3 >★★★ 下单>10次</option>
						 <option value=4 >★★★★ 下单>20次</option>
						 <option value=5 >★★★★★ 下单>30次</option>
						 </select></td></tr>";
	$strTR = $strTR."<tr ><td>Site Name</td>";
	$strTR = $strTR."<td><select name='site_name' id='site_name'>
					<option value='CivilLaser' >CivilLaser</option>
					<option value='CivilLaser-JP' >CivilLaser-JP</option>
					<option value='LuckLaser' >LuckLaser</option>
					<option value='LuckLaser-JP' >LuckLaser-JP</option>
					<option value='LuckLaser-KR' >LuckLaser-KR</option>
					<option value='HitecLaesr' >HitecLaesr</option>
					<option value='Aliexpress' >Aliexpress</option>
					 </select></td></tr>";
	$strTR = $strTR."<tr ><td>Produc Name</td>";
	$strTR = $strTR."<td><select name='product_name' id='product_name'>
						<option value='Laser Module' >Laser Module</option>
						<option value='DPSS Laser' >DPSS Laser</option>
						<option value='Laser Pointer' >Laser Pointer</option>
						<option value='Laser Stage' >Laser Stage</option>
						<option value='Laser Sight' >Laser Sight</option>
					   </select></td></tr>";
    $strTR = $strTR."<tr ><td>Note</td>";
	$strTR = $strTR."<td><textarea  cols='60'  rows='5' name='note' id='note' ></textarea></td></tr>";
	$strTR = $strTR."<tr><td colspan=2 align=center>";
	$strTR = $strTR."<input type='button' value='保存' onclick='insertCustomer()'>";
	$strTR = $strTR."&nbsp;&nbsp;";
	$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
	$strTR = $strTR."</td></tr>";
	echo $strTR;
}else{	
	if(!$result->EOF){
		//********客户VIP等级*************
		$vip_level = $result->fields['vip_level'];
		if($vip_level == "1"){
			$vip_level_1 = "selected";
		}else if($vip_level == "2"){
			$vip_level_2 = "selected";
		}else if($vip_level == "3"){
			$vip_level_3 = "selected";
		}else if($vip_level == "4"){
			$vip_level_4 = "selected";
		}else if($vip_level == "5"){
			$vip_level_5 = "selected";
		}
		//********客户来自哪个网站*************
		$site_name = $result->fields['site_name'];
		if($site_name == "CivilLaser"){
			$site_name_CivilLaser = "selected";
		}else if($site_name == "CivilLaser-JP"){
			$site_name_CivilLaser_JP = "selected";
		}else if($site_name == "LuckLaser"){
			$site_name_LuckLaser = "selected";
		}else if($site_name == "LuckLaser-JP"){
			$site_name_LuckLaser_JP = "selected";
		}else if($site_name == "LuckLaser-KR"){
			$site_name_LuckLaser_KR = "selected";
		}else if($site_name == "HitecLaesr"){
			$site_name_HitecLaesr = "selected";
		}else if($site_name == "Aliexpress"){
			$site_name_Aliexpress = "selected";
		}
		//********客户订哪类产品*************
		$product_name = $result->fields['product_name'];
		if($product_name == "Laser Module"){
			$product_name_LaserModule = "selected";
		}else if($product_name == "DPSS Laser"){
			$product_name_DPSSLaser = "selected";
		}else if($product_name == "Laser Pointer"){
			$product_name_LaserPointer = "selected";
		}else if($product_name == "Laser Stage"){
			$product_name_LaserStage = "selected";
		}else if($product_name == "Laser Sight"){
			$product_name_LaserSight = "selected";
		}
		//********Display*************
		$strTR = "";
		$strTR = $strTR."<tr ><td>ID</td>";
		$strTR = $strTR."<td>".$result->fields['id']."</td></tr>";
		$strTR = $strTR."<tr ><td>Create Time</td>";
		$strTR = $strTR."<td>".$result->fields['create_time']."</td></tr>";
		$strTR = $strTR."<tr ><td>First Name</td>";
		$strTR = $strTR."<td><input type='text' name='firstname' id='firstname' value='".$result->fields['firstname']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>Last Name</td>";
		$strTR = $strTR."<td><input type='text' name='lastname' id='lastname' value='".$result->fields['lastname']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>Email</td>";
		$strTR = $strTR."<td><input type='text' name='email_address' id='email_address' value='".$result->fields['email_address']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>Street Address</td>";
		$strTR = $strTR."<td><input type='text' name='street_address' id='street_address' value='".$result->fields['street_address']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>City</td>";
		$strTR = $strTR."<td><input type='text' name='city' id='city' value='".$result->fields['city']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>State</td>";
		$strTR = $strTR."<td><input type='text' name='state' id='state' value='".$result->fields['state']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>Country</td>";
		$strTR = $strTR."<td><input type='text' name='country' id='country' value='".$result->fields['country']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>Post code</td>";
		$strTR = $strTR."<td><input type='text' name='postcode' id='postcode' value='".$result->fields['postcode']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>Telephone Number</td>";
		$strTR = $strTR."<td><input type='text' name='tel_number' id='tel_number' value='".$result->fields['tel_number']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>VIP Level</td>";
		$strTR = $strTR."<td><select name='vip_level' id='vip_level'>
							 <option value=1 ".$vip_level_1." >★ 下单>2次or>$500</option>
							 <option value=2 ".$vip_level_2." >★★ 下单>5次</option>
							 <option value=3 ".$vip_level_3." >★★★ 下单>10次</option>
							 <option value=4 ".$vip_level_4." >★★★★ 下单>20次</option>
							 <option value=5 ".$vip_level_5." >★★★★★ 下单>30次</option>
							 </select></td></tr>";
		$strTR = $strTR."<tr ><td>Site Name</td>";
		$strTR = $strTR."<td><select name='site_name' id='site_name'>
						<option value='CivilLaser' ".$site_name_CivilLaser."  >CivilLaser</option>
						<option value='CivilLaser-JP' ".$site_name_CivilLaser_JP."  >CivilLaser-JP</option>
						<option value='LuckLaser' ".$site_name_LuckLaser."  >LuckLaser</option>
						<option value='LuckLaser-JP' ".$site_name_LuckLaser_JP."  >LuckLaser-JP</option>
						<option value='LuckLaser-KR' ".$site_name_LuckLaser_KR."  >LuckLaser-KR</option>
						<option value='HitecLaesr' ".$site_name_HitecLaesr."  >HitecLaesr</option>
						<option value='Aliexpress' ".$site_name_Aliexpress."  >Aliexpress</option>
						 </select></td></tr>";
		$strTR = $strTR."<tr ><td>Produc Name</td>";
		$strTR = $strTR."<td><select name='product_name' id='product_name'>
							<option value='Laser Module' ".$product_name_LaserModule."  >Laser Module</option>
							<option value='DPSS Laser'  ".$product_name_DPSSLaser." >DPSS Laser</option>
							<option value='Laser Pointer' ".$product_name_LaserPointer.">Laser Pointer</option>
							<option value='Laser Stage' ".$product_name_LaserStage." >Laser Stage</option>
							<option value='Laser Sight' ".$product_name_LaserSight." >Laser Sight</option>
						   </select></td></tr>";
		$strTR = $strTR."<tr ><td>Note</td>";
		$strTR = $strTR."<td><textarea  cols='60'  rows='5' name='note' id='note' >".$result->fields['note']."</textarea></td></tr>";

		$strTR = $strTR."<tr><td colspan=2 align=center>";
		$strTR = $strTR."<input type='button' value='保存' onclick='updateCustomer(".$id.")'>";
		$strTR = $strTR."&nbsp;&nbsp;";
		$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
		$strTR = $strTR."</td></tr>";
		echo $strTR;
	}
}
?>

</table>



</div>

<br><br>
<br><br>

</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" >
<input type="hidden" name="id" id="id" >
</form>
</body>




</html>

<script>
<?php
if($doFlag == "insertCustomer"){
	echo "window.location.href = 'customerList.php';"; 
}else if($doFlag == "updateCustomer"){
	echo "window.location.href = 'customerInput.php?id=".$id."';"; 
}

?>

function toList(){
	window.location.href = "customerList.php";
}


function insertCustomer(){
	document.getElementById('doFlag').value = 'insertCustomer';	
	document.inputForm.submit();
}

function updateCustomer(id){
	document.getElementById('doFlag').value = 'updateCustomer';	
	document.getElementById('id').value = id;
	document.inputForm.submit();
}


</script>

